<template>
  <div>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加部门</el-button
          >
        </el-col>
      </el-row>

      <!-- 部门列表区域 -->
      <!-- <el-table :data="departmentList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="部门名称" prop="name"></el-table-column>
        <el-table-column label="标签">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.type == 1 ? 'primary' : 'success'"
              disable-transitions
              >{{ getTagInfo(scope.row.type)['label'] }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeDepartById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table> -->

      <!-- 树形部门列表 -->
      <tree-table
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        show-index
        border
        :expand-type="false"
      >
        <template slot="sign" slot-scope="scope">
          <el-tag
            :type="scope.row.type == 1 ? 'primary' : 'success'"
            disable-transitions
            >{{ scope.row.name }}</el-tag
          >
        </template>
        <template slot="opt" slot-scope="scope">
          <!-- 修改按钮 -->
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="showEditDialog(scope.row.id)"
          ></el-button>
          <!-- 删除按钮 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="removeDepartById(scope.row.id)"
          ></el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <!-- <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination> -->
    </el-card>

    <!-- 修改部门的对话框 -->
    <el-dialog
      title="修改部门"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <el-form
        :model="editForm"
        :rules="FormRules"
        ref="editFormRef"
        label-width="85px"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="部门类型" prop="type">
          <el-select
            v-model="editForm.type"
            placeholder="请选择部门类型"
            @change="isShowGaozhi"
          >
            <el-option
              v-for="item in departmentTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否是高职学院" prop="is_gaozhi" v-if="isShowGz">
          <el-switch
            v-model="addForm.is_gaozhi"
            active-text="是"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDepartment">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加部门的对话框 -->
    <el-dialog
      title="添加部门"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <el-form
        :model="addForm"
        :rules="FormRules"
        ref="addFormRef"
        label-width="85px"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="部门类型" prop="type">
          <el-select
            v-model="addForm.type"
            placeholder="请选择部门类型"
            @change="isShowGaozhi"
          >
            <el-option
              v-for="item in departmentTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否是高职学院" prop="is_gaozhi" v-if="isShowGz">
          <el-switch
            v-model="addForm.is_gaozhi"
            active-text="是"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDepartment">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDepartment,
  addDepartment,
  removeDepartment,
  getDepartmentItem,
  editDepartmentItem,
  getDepartmentType,
  getTreeDepartment,
} from '@/api/department'
export default {
  data() {
    return {
      prop: {
        label: 'name',
        children: 'children',
        checkStrictly: true,
        value: 'id',
      },
      columns: [
        {
          label: '部门名称',
          prop: 'name',
        },
        {
          label: '标签',
          type: 'template',
          template: 'sign',
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt',
        },
      ],
      cateList: [],
      // 获取部门列表的参数对象
      queryInfo: {
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 5,
      },
      departmentList: [],
      departmentTypeOptions: [],
      total: 0,
      // 添加部门的表单数据
      addForm: {
        is_gaozhi: 0,
        name: '',
        type: '',
      },
      editForm: {
        is_gaozhi: '',
        name: '',
        type: '',
      },
      // 添加、修改表单的验证规则对象
      FormRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '部门名的长度在3~10个字符之间',
            trigger: 'blur',
          },
        ],
        type: [
          { required: true, message: '请选择部门类型', trigger: 'change' },
        ],
      },
      isShowGz: false,
    }
  },
  created() {
    this.getDepartmentTypeLit()
    this.getTreeList()
  },
  methods: {
    async getTreeList() {
      var res = await getTreeDepartment()
      console.log('tree', res)
      this.cateList = res
    },
    getTagInfo(type) {
      let [tag] = this.departmentTypeOptions.filter(
        (item) => item.value == type
      )
      return tag
    },
    async getDepartmentTypeLit() {
      const result = await getDepartmentType()
      if (result.length) {
        result.forEach((item) => {
          this.departmentTypeOptions.push({
            value: item.id,
            label: item.name,
          })
        })
      }
      this.getDepartmentList()
    },
    // async getDepartmentList() {
    //   const result = await getDepartment(this.queryInfo)
    //   console.log(result.data)
    //   if (result) {
    //     this.departmentList = result.data //列表中展示的数据内容
    //     this.total = result.count //数据总条数
    //   }
    // },


    // 监听 pagesize 改变的事件
    // handleSizeChange(newSize) {
    //   this.queryInfo.pagesize = newSize
    //   this.getDepartmentList()
    // },


    // 监听 页码值 改变的事件


    // handleCurrentChange(newPage) {
    //   this.queryInfo.pagenum = newPage
    //   this.getDepartmentList()
    // },

    isShowGaozhi(val) {
      if (val == 1) {
        this.isShowGz = true
      } else {
        this.isShowGz = false
      }
    },

    addDepartment() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return
        // 可以发起添加部门的网络请求
        console.log(this.addForm)
        const result = await addDepartment(this.addForm)
        if (result.name) {
          this.addDialogClosed()
          this.$message.success('添加部门成功！')
          // this.getDepartmentList()
          this.getTreeList()
        }
      })
    },

    // 展示编辑部门的对话框
    async showEditDialog(id) {
      const result = await getDepartmentItem(id)
      if (result) {
        this.editForm = result
        this.editDialogVisible = true
      }
    },

    // 修改部门信息并提交
    editDepartment() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return
        // 发起修改部门信息的数据请求
        const result = await editDepartmentItem(this.editForm)

        if (result) {
          // 关闭对话框
          this.editDialogVisible = false
          // 刷新数据列表
          // this.getDepartmentList()
          this.getTreeList()
          // 提示修改成功
          this.$message.success('更新部门信息成功！')
        }
      })
    },
    // 根据Id删除对应的部门信息
    async removeDepartById(id) {
      // 弹框询问部门是否删除数据
      const confirmResult = await this.$confirm(
        '此操作将永久删除该部门, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)

      // 如果部门确认删除，则返回值为字符串 confirm
      // 如果部门取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      removeDepartment(id).then((value) => {
        this.$message.success('删除部门成功！')
        // this.getDepartmentList()
        this.getTreeList()
      })
    },
  },
}
</script>

<style lang="less" scoped>
</style>
